<template>
	<view class="uni-numbox">
		<view class="comm-uni-numbox uni-numbox-minus"
			@click="_calcValue('subtract')" >
			<text class="icon-font imiicon-com imi-icon" :class="minDisabled?'uni-numbox-disabled': ''">&#xe65b;</text>
		</view>
		<input class="uni-numbox-value" :style="'width: '+ input_width +'px;'"
			type="number" 
			:disabled="disabled"
			:value="inputValue" 
			@blur="_onBlur"
			@focus="numberFocus"
			@click="numberFocus"
			@input="inputHandler">
		<view class="comm-uni-numbox uni-numbox-plus" @click="_calcValue('add')">
			<text class="icon-font imiicon-com imi-icon" :class="maxDisabled?'uni-numbox-disabled': ''" >&#xe606;</text>
		</view>
	</view>
</template>
<script>
	import util from "../../js/util/Util.js";
	
	let clickTime = new Date().getTime();
	const MIN_CLICK_TIME = 300; 
	export default {
		// name: 'uni-number-box',
		props: {
			isMax: {
				type: Boolean,
				default: false
			},
			isMin: {
				type: Boolean,
				default: false
			},
			value: {
				type: Number,
				default: 0
			},
			min: {
				type: Number,
				default: 1
			},
			max: {
				type: Number,
				default: 100000000
			},
			step: {
				type: Number,
				default: 1
			},
			
			disabled: {
				type: Boolean,
				default: false
			},
			
			num_p:
			{
				type:Number,   
				default:0
			},
			
			input_width:
			{
				type:Number,
				default:130
			},
			index:
			{
				type:Number,
				default:0
			}
		},
		
		data() {
			return {
				inputValue: this.value,
				minDisabled: false,
				maxDisabled: false,
				isUpdate:false,
			}
		},
		created(){
			this.maxDisabled = this.isMax;
			this.minDisabled = this.isMin;
			//this.inputValue = this.min;
			if(this.inputValue == 0)
				this.minDisabled = true;
		},
		computed: {
			
		},
		watch: {
			inputValue(number) {
				const data = {
					number : number,
					index : this.index
				}
				
				//设置数据不调用外部更新
				if(this.isUpdate)
				{
					return;
				}
				this.$emit('eventChange', data);
			},
		},
		methods: {
			numberFocus()
			{
				this.$emit("numberFocus");
			},
			
			updateValue(val)
			{
				this.isUpdate = true;
				this.inputValue = val;
				this.setMinDisabled();
				if(val == 0)
					this.minDisabled = true;
			},
			
			async _calcValue(type) {
				this.isUpdate = false;
				let nowTime = new Date().getTime();
				if(nowTime - clickTime <= MIN_CLICK_TIME)
				{
					console.log("小于"+ MIN_CLICK_TIME +"毫秒！！");
					return;
				}
				clickTime = nowTime;
				const scale = this._getDecimalScale();
				let value = this.inputValue * scale;
			
				let newValue = this.min;
				let step = this.step * scale;
				if(type === 'subtract')
				{
					newValue = value - step;
					if (newValue < this.min)
					{
						this.minDisabled = true;
						newValue = 0;
					}
				}
				else if(type === 'add')
				{
					if(value == 0 && newValue == this.min)
					{
						newValue = this.min;
					}
					else
					{
						newValue = value + step;
					}
				
					if (newValue >= this.max)
					{
						this.maxDisabled = true;
					}
					
					if(newValue > this.max)
					{
						newValue = this.max;
					}
					if(newValue >= this.min)
					{
						this.minDisabled = false;
					}
				}
				
				if( this.num_p > 0)
				{
					this.inputValue = util.decimalPlaceCount(newValue / scale,this.num_p);
				}
				else
				{
					this.inputValue = newValue / scale;
				}
			},
			
			_getDecimalScale()
			{
				let scale = 1;
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, (this.step + '').split('.')[1].length);
				}
				return scale;
			},
			
			_onBlur(event) {
				let value = event.detail.value;
				if (!value) {
					this.inputValue = 0;
					return;
				}
				value = +value;
				if (value > this.max) {
					value = this.max;
				} else if (value < this.min) {
					value = this.min;
					this.minDisabled = false;
				}
				this.inputValue = value;
			},
			
			inputHandler(event)
			{
				this.isUpdate = false;
				let newValue = parseInt(event.target.value);
				if(isNaN(newValue))
					newValue = 0;
				this.inputValue = newValue;
				this.setMinDisabled();
			},
			
			setMinDisabled(val)
			{
				if(val < this.min)
				{
					this.minDisabled = true;
				}
				else
				{
					this.minDisabled = false;
				}
			},
			
			setValue(value)
			{
				this.isUpdate = true;
				this.inputValue = value;
				this.setMinDisabled(value);
			}
			
			
		}
	}
</script>
<style>
	.uni-numbox 
	{
		height: 30px;
		display: flex;
	}
	.comm-uni-numbox{
		font-size : 12px;
		width: 25px;
		height: 25px;
		text-align: center;
		cursor: pointer;
		position: relative;
	}
	.uni-numbox-minus .imiicon-com,
	{
		font-weight: bold;
		position: absolute;
		left: 13px;
	}
	.uni-numbox-plus .imiicon-com
	{
		font-weight: bold;
		position: absolute;
		left: 4px;
	}
	
	.uni-numbox-minus .imi-icon
	{
		color: #333;
		padding-left: 5px;
	}
	.uni-numbox-plus .imi-icon
	{
		color: #fff;
		padding-left: 3px;
	}

	.uni-numbox-minus 
	{
		border: 1px solid #EAEAEA;
		background-color: #EFEFEF;
		border-top-right-radius: 0px;
		border-top-left-radius: 4px;
		border-bottom-right-radius: 0px;
		border-bottom-left-radius: 4px;
	}

	.uni-numbox-plus
	{
		border: 1px solid #3388FF;
		background-color: #3388FF;
		border-top-right-radius: 4px;
		border-top-left-radius: 0px;
		border-bottom-right-radius: 4px;
		border-bottom-left-radius: 0px;
	}

	.uni-numbox-value 
	{
		height: 25px;
		text-align: center;
		padding: 0;
		font-size: 15px;
		color:#333;
		border-top: #EFEFEF 1px solid;
		border-bottom: #EFEFEF 1px solid;
		background-color: #FFF;
	}
	.uni-numbox-disabled.imi-icon 
	{
		color: #d6d6d6;
	}
</style>
